
import java.awt.Graphics2D;
import java.util.List;

public class Hero extends Player {
    public FloatPoint position;      //浮点位置
    public int direction;            //方向
    //direction 0:枪口向上  2：枪口右  6：枪口左  1：右上  3：右下     5：左下  7：左上
//    private static int ANIMATION_TEMP = 0;
    private static float JUMP_HEIGHT = 0.0F;   //跳的高度
    public boolean jumping = false;
    public boolean jumpFinished = true;
    public boolean canShot = true;
    public boolean canDrop = true;
    public boolean towardsLeft = false;
    public boolean towardsRight = true;
    public boolean visible = false;        //明显的 看得见的
    public int state = 0;
    //状态  0：站立  1：走路   2：跳跃  3：爬行   4：死亡  5：掉落
    public Weapon weapon;
    public int deathEventType;
    public int width;
    public int height;
    int visibleTemp = 0;
    int temp = 0;

    public Hero() {
        this.direction = 2;
        this.position = new FloatPoint(0.0F, 0.0F);
        this.weapon = new SimpleWeapon(1);
    }

    public Hero(FloatPoint position) {
        this.direction = 2;
        this.position = position;
        this.weapon = new SimpleWeapon(1);
    }

    public Hero(FloatPoint position, int direction) {
        this.state = 0;
        this.position = position;
        this.direction = direction;
        this.weapon = new SimpleWeapon(1);
    }

    public void drawPlayer(Graphics2D g, float mapPosition) {
        if (this.visibleTemp++ > 180)
            this.visible = true;
        if (this.state == 0) {  //站立
            this.width = 80;
            this.height = 90;
            standAnimation(g, mapPosition);
        } else if (this.state == 1) {       // 1：走路
            this.width = 60;
            this.height = 90;
            walkAnimation(g, mapPosition);
        } else if (this.state == 2) {       //2：跳跃
            this.width = 66;
            this.height = 66;
            jumpAnimation(g, mapPosition);
        } else if (this.state == 3) {       //3：爬行
            this.width = 100;
            this.height = 30;
            crawlAnimation(g, mapPosition);
        } else if (this.state == 4) {       // 4：死亡
            dieingAnimation(g, mapPosition);
        } else if (this.state == 6) {       //5：掉落
            this.width = 60;
            this.height = 90;
            droppingAnimation(g, mapPosition);
        }
    }

    private void droppingAnimation(Graphics2D g, float mapPosition) {   //掉落方法
        if ((this.direction == 0) && (this.towardsRight)) {     //朝左掉落 枪口向上
            g.drawImage(Imgs.STAND02, (int) ((this.position.x - mapPosition) * 3.0F) - 27, (int) (this.position.y * 3.0F) - 126, null);
        } else if ((this.direction == 0) && (this.towardsLeft)) {     //朝右掉落  枪口向上
            g.drawImage(Imgs.STAND06, (int) ((this.position.x - mapPosition) * 3.0F) - 27, (int) (this.position.y * 3.0F) - 126, null);
        } else if (this.direction == 1) {                   //枪口右上
            g.drawImage(Imgs.WALKANDSHOT1, (int) ((this.position.x - mapPosition) * 3.0F) - 45, (int) (this.position.y * 3.0F) - 100, (int) ((this.position.x - mapPosition) * 3.0F) + 45, (int) (this.position.y * 3.0F), 90, 0, 180, 100, null);
        } else if (this.direction == 2) {                     //枪口朝右
            g.drawImage(Imgs.STAND2, (int) ((this.position.x - mapPosition) * 3.0F) - 20, (int) (this.position.y * 3.0F) - 90, null);
        } else if (this.direction == 3) {                     //枪口右下
            g.drawImage(Imgs.WALKANDSHOT3, (int) ((this.position.x - mapPosition) * 3.0F) - 40, (int) (this.position.y * 3.0F) - 90, (int) ((this.position.x - mapPosition) * 3.0F) + 40, (int) (this.position.y * 3.0F), 80, 0, 160, 90, null);
        } else if (this.direction == 5) {                     //枪口左下
            g.drawImage(Imgs.WALKANDSHOT5, (int) ((this.position.x - mapPosition) * 3.0F) - 40, (int) (this.position.y * 3.0F) - 90, (int) ((this.position.x - mapPosition) * 3.0F) + 40, (int) (this.position.y * 3.0F), 80, 0, 160, 90, null);
        } else if (this.direction == 6) {                     //枪口向左
            g.drawImage(Imgs.STAND6, (int) ((this.position.x - mapPosition) * 3.0F) - 40, (int) (this.position.y * 3.0F) - 90, null);
        } else if (this.direction == 7)                       //枪口左下
            g.drawImage(Imgs.WALKANDSHOT7, (int) ((this.position.x - mapPosition) * 3.0F) - 45, (int) (this.position.y * 3.0F) - 100, (int) ((this.position.x - mapPosition) * 3.0F) + 45, (int) (this.position.y * 3.0F), 90, 0, 180, 100, null);
    }

    private void dieingAnimation(Graphics2D g, float mapPosition)    //在我的代码中删除了这几行  判断无用
    {
        this.jumpFinished = false;
        if (this.temp++ < 16) {
            if (this.towardsRight) {
                this.position.x -= 1.666667F;
                this.position.y -= 1.666667F;
                g.drawImage(Imgs.HERODIEING, (int) (this.position.x - mapPosition) * 3 - 58, (int) (this.position.y * 3.0F) - 45, (int) (this.position.x - mapPosition) * 3 + 58, (int) (this.position.y * 3.0F), 0, 0, 90, 35, null);
            } else if (this.towardsLeft) {
                this.position.x += 1.666667F;
                this.position.y -= 1.666667F;
                g.drawImage(Imgs.HERODIEING, (int) (this.position.x - mapPosition) * 3 - 58, (int) (this.position.y * 3.0F) - 45, (int) (this.position.x - mapPosition) * 3 + 58, (int) (this.position.y * 3.0F), 0, 0, 90, 35, null);
            }
        } else if (this.temp++ < 32) {
            if (this.towardsRight) {
                this.position.x -= 1.666667F;
                this.position.y += 1.666667F;
                g.drawImage(Imgs.HERODIEING, (int) (this.position.x - mapPosition) * 3 - 58, (int) (this.position.y * 3.0F) - 45, (int) (this.position.x - mapPosition) * 3 + 58, (int) (this.position.y * 3.0F), 0, 0, 90, 35, null);
            } else if (this.towardsLeft) {
                this.position.x += 1.666667F;
                this.position.y += 1.666667F;
                g.drawImage(Imgs.SIMPLEENEMYDIEING6, (int) (this.position.x - mapPosition) * 3 - 58, (int) (this.position.y * 3.0F) - 45, (int) (this.position.x - mapPosition) * 3 + 58, (int) (this.position.y * 3.0F), 0, 0, 90, 35, null);
            }
        } else if (this.temp < 70) {
            g.drawImage(Imgs.HERODIEING, (int) (this.position.x - mapPosition) * 3 - 58, (int) (this.position.y * 3.0F), (int) (this.position.x - mapPosition) * 3 + 58, (int) (this.position.y * 3.0F) + 40, 90, 0, 180, 35, null);
        } else
            this.state = 5;
    }

    //画站着动作
    public void standAnimation(Graphics2D g, float mapPosition) {
//        ANIMATION_TEMP = 0;
        this.jumping = false;
        this.jumpFinished = true;
        if (this.direction == 2) {      //向右走路
            g.drawImage(Imgs.STAND2, (int) ((this.position.x - mapPosition) * 3.0F) - 20, (int) (this.position.y * 3.0F) - 90, null);
        } else if (this.direction == 6) {     //朝左站立举枪
            g.drawImage(Imgs.STAND6, (int) ((this.position.x - mapPosition) * 3.0F) - 40, (int) (this.position.y * 3.0F) - 90, null);
        } else if ((this.direction == 0) && (this.towardsRight)) {    //枪口向上朝右 站立
            g.drawImage(Imgs.STAND02, (int) ((this.position.x - mapPosition) * 3.0F) - 27, (int) (this.position.y * 3.0F) - 126, null);
        } else if ((this.direction == 0) && (this.towardsLeft))       //枪口向上朝左 站立
            g.drawImage(Imgs.STAND06, (int) ((this.position.x - mapPosition) * 3.0F) - 27, (int) (this.position.y * 3.0F) - 126, null);
    }

    //画走路动作
    public void walkAnimation(Graphics2D g, float mapPosition) {
        this.jumping = false;
        this.jumpFinished = true;
//        int temp = ANIMATION_TEMP / 10;
        if (this.direction == 2) {      //向右走路
            g.drawImage(Imgs.WALKANDSHOT2, (int) ((this.position.x - mapPosition) * 3.0F) - 45, (int) (this.position.y * 3.0F) - 90, (int) ((this.position.x - mapPosition) * 3.0F) + 45, (int) (this.position.y * 3.0F), temp * 90, 0, temp * 90 + 90, 90, null);
        } else if (this.direction == 6) {     //向左走路
            g.drawImage(Imgs.WALKANDSHOT6, (int) ((this.position.x - mapPosition) * 3.0F) - 45, (int) (this.position.y * 3.0F) - 90, (int) ((this.position.x - mapPosition) * 3.0F) + 45, (int) (this.position.y * 3.0F), (3 - temp) * 90, 0, (3 - temp) * 90 + 90, 90, null);
        } else if (this.direction == 3) {     //右下走路
            g.drawImage(Imgs.WALKANDSHOT3, (int) ((this.position.x - mapPosition) * 3.0F) - 40, (int) (this.position.y * 3.0F) - 90, (int) ((this.position.x - mapPosition) * 3.0F) + 40, (int) (this.position.y * 3.0F), temp * 80, 0, temp * 80 + 80, 90, null);
        } else if (this.direction == 5) {     //左下走路
            g.drawImage(Imgs.WALKANDSHOT5, (int) ((this.position.x - mapPosition) * 3.0F) - 40, (int) (this.position.y * 3.0F) - 90, (int) ((this.position.x - mapPosition) * 3.0F) + 40, (int) (this.position.y * 3.0F), (3 - temp) * 80, 0, (3 - temp) * 80 + 80, 90, null);
        } else if (this.direction == 1) {     //右上走路
            g.drawImage(Imgs.WALKANDSHOT1, (int) ((this.position.x - mapPosition) * 3.0F) - 45, (int) (this.position.y * 3.0F) - 100, (int) ((this.position.x - mapPosition) * 3.0F) + 45, (int) (this.position.y * 3.0F), temp * 90, 0, temp * 90 + 90, 100, null);
        } else if (this.direction == 7) {     //左上走路
            g.drawImage(Imgs.WALKANDSHOT7, (int) ((this.position.x - mapPosition) * 3.0F) - 45, (int) (this.position.y * 3.0F) - 100, (int) ((this.position.x - mapPosition) * 3.0F) + 45, (int) (this.position.y * 3.0F), (3 - temp) * 90, 0, (3 - temp) * 90 + 90, 100, null);
        }
//        if (++ANIMATION_TEMP > 30)
//            ANIMATION_TEMP = 0;
    }

    public void jumpAnimation(Graphics2D g, float mapPosition) {
//     int temp = ANIMATION_TEMP / 3;
        if (        //向右跳
                (this.direction == 0) || (this.direction == 1) || (this.direction == 2) || (this.direction == 3)) {
            g.drawImage(Imgs.JUMP2, (int) ((this.position.x - mapPosition) * 3.0F) - 33, (int) (this.position.y * 3.0F) - 66, (int) ((this.position.x - mapPosition) * 3.0F) + 33, (int) (this.position.y * 3.0F), temp * 66, 0, temp * 66 + 66, 66, null);
        }       //向左跳
        else if ((this.direction == 4) || (this.direction == 5) || (this.direction == 6) || (this.direction == 7)) {
            g.drawImage(Imgs.JUMP6, (int) ((this.position.x - mapPosition) * 3.0F) - 33, (int) (this.position.y * 3.0F) - 66, (int) ((this.position.x - mapPosition) * 3.0F) + 33, (int) (this.position.y * 3.0F), (7 - temp) * 66, 0, (7 - temp) * 66 + 66, 66, null);
        }
//        if (++ANIMATION_TEMP > 21) {
//            ANIMATION_TEMP = 0;
//        }
        if ((JUMP_HEIGHT < 30.0F) && (!this.jumpFinished)) {
            JUMP_HEIGHT += 1.0F;
            this.position.y -= 2.0F;
        } else {
            this.jumpFinished = true;
            JUMP_HEIGHT = 0.0F;
            this.position.y += 2.0F;
        }
    }

    //爬行
    private void crawlAnimation(Graphics2D g, float mapPosition) {
        //ANIMATION_TEMP = 0;
        this.jumping = false;
        this.jumpFinished = false;
        if (this.direction == 6) {      //左趴
            g.drawImage(Imgs.CRAWL6, (int) ((this.position.x - mapPosition) * 3.0F) - 55, (int) (this.position.y * 3.0F) - 42, null);
        } else if (this.direction == 2)       //右趴
            g.drawImage(Imgs.CRAWL2, (int) ((this.position.x - mapPosition) * 3.0F) - 55, (int) (this.position.y * 3.0F) - 42, null);
    }

    public List<Bullet> shot(int num) {    //hero射击方法
        if (this.canShot) {
            if (this.state == 3) {           //爬行
                if (this.towardsRight) {        //爬行朝右
                    return this.weapon.shot(new FloatPoint(this.position.x + 3.333333F, this.position.y + 12.333333F), this.direction, num);
                } else {                             //爬行朝左
                    return this.weapon.shot(new FloatPoint(this.position.x - 3.333333F, this.position.y + 12.333333F), this.direction, num);
                }
            }

            if (this.state == 0) {         //站立
                if (this.direction == 0) {   //枪口向上
                    if (this.towardsRight) {     //朝右
                        return this.weapon.shot(new FloatPoint(this.position.x + 4.333334F, this.position.y - 22.333334F), this.direction, num);
                    } else {                       //枪口向上朝左
                        return this.weapon.shot(new FloatPoint(this.position.x - 4.333334F, this.position.y - 22.333334F), this.direction, num);
                    }
                }

                if (this.towardsRight) {     //站立朝右
                    return this.weapon.shot(new FloatPoint(this.position.x + 5.0F, this.position.y), this.direction, num);
                } else {                       //站立朝左
                    return this.weapon.shot(new FloatPoint(this.position.x - 5.0F, this.position.y), this.direction, num);
                }
            }

            if (this.state == 2) {       //跳跃
                return this.weapon.shot(new FloatPoint(this.position.x, this.position.y - 10.0F), this.direction, num);
            } else {
                return this.weapon.shot(new FloatPoint(this.position.x, this.position.y), this.direction, num);
            }
        } else {
            return null;
        }

    }
}
